package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.jstyle.jclife.R;
import com.jstyle.jclife.model.SleepDetail;
import com.jstyle.jclife.utils.DateUtil;

import java.text.NumberFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuge on 2017/2/25 0025.
 */

public class SleepDetailAdapter extends RecyclerView.Adapter {
    private static final String TAG = "MusicAdapter";
    List<SleepDetail> sleepDetails;
    private boolean isNoData=true;

    public SleepDetailAdapter(List<SleepDetail> sleepDetails) {
        this.sleepDetails = sleepDetails;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sleepdetail, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final SleepDetail sleepDetail = sleepDetails.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        if(position==1){
            isNoData=sleepDetail.getContent()==0;
        }

        viewHolder.tvSleepDetialContent.setText(getContentString(position,sleepDetail.getContent(),holder.itemView.getContext()));
        viewHolder.tvSleepDetialTitle.setText(sleepDetail.getTitle());
        viewHolder.tvSleepDetialLimit.setText(sleepDetail.getLimit());
        if (!isNoData&&!isLimit(position, sleepDetail.getContent())) {
            viewHolder.tvSleepDetialContent.setTextColor(Color.parseColor("#fdbb30"));
        } else {
            viewHolder.tvSleepDetialContent.setTextColor(Color.parseColor("#415daa"));
        }
    }

    private String getContentString(int position, float value, Context context) {
        String content = "";
        switch (position) {
            case 0:
                content = context.getResources().getString(R.string.Value);
                break;
            case 1:
                content = DateUtil.getPointSleepTime(context,value);
                break;
            case 2:
                content = DateUtil.getPointSleepTime(context,value);
                break;
            case 3:
                content = DateUtil.getPointSleepTime(context,value);
                break;

            case 4:
                NumberFormat nf = NumberFormat.getPercentInstance();
                nf.setMinimumFractionDigits(0);
                content = String.format(context.getString(R.string.format_sleepEfficiency), nf.format(value));
                break;
            case 5:
                NumberFormat format= NumberFormat.getNumberInstance();
                format.setMinimumFractionDigits(0);
                content = String.format(context.getString(R.string.format_AverageHeartRate),format.format(value));
                break;

        }
        return content;


    }

    private boolean isLimit(int position, float value) {
        boolean isLimit = true;
        switch (position) {
            case 1:
                if (value < 5.5*60 || value > 9*60) isLimit = false;
                break;
            case 2:
                if (value < 1 *60|| value > 2.5*60) isLimit = false;
                break;
            case 3:
                if (value < 2.5*60 || value > 4.8*60) isLimit = false;
                break;
            case 4:
                if (value < 75/100f || value > 95/100f) isLimit = false;
                break;
            case 5:
                if (value < 55 || value > 80) isLimit = false;
                break;
        }
        return isLimit;
    }


    public void setData(List<SleepDetail> sleepDetails) {
        this.sleepDetails = sleepDetails;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return sleepDetails.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sleepDetial_title)
        TextView tvSleepDetialTitle;
        @BindView(R.id.tv_sleepDetial_content)
        TextView tvSleepDetialContent;
        @BindView(R.id.tv_sleepDetial_limit)
        TextView tvSleepDetialLimit;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
